//index.js 
 
var util = require('../../utils/util.js') 
var app = getApp() 
Page({ 
  data: { 
    feed: [], 
    feed_length: 0 
  }, 
  //事件处理函数 
  bindItemTap: function() { 
    wx.navigateTo({ 
      url: '../ANS1/ans1' 
    }) 
  }, 
  bindQueTap: function() { 
    wx.navigateTo({ 
      url: '../user/user' 
    }) 
  }, 
  bindAddTap: function() {
    wx.navigateTo({
      url: '../edit/edit'
    })
  },
  onLoad: function () { 
    console.log('onLoad') 
    var that = this 
    //调用应用实例的方法获取全局数据 
    this.getData(); 
    this.getShareList();//网络请求
  }, 
  upper: function () { 
    wx.showNavigationBarLoading() 
    this.refresh(); 
    console.log("upper"); 
    setTimeout(function(){wx.hideNavigationBarLoading();wx.stopPullDownRefresh();}, 2000); 
  }, 
  lower: function (e) { 
    wx.showNavigationBarLoading(); 
    var that = this; 
    setTimeout(function(){wx.hideNavigationBarLoading();that.nextLoad();}, 1000); 
    console.log("lower") 
  }, 
  //scroll: function (e) { 
  //  console.log("scroll") 
  //}, 
 
  //网络请求数据, 实现首页刷新 
  refresh0: function(){ 
    var index_api = ''; 
    util.getData(index_api) 
        .then(function(data){ 
          //this.setData({ 
          // 
          //}); 
          console.log(data); 
        }); 
  }, 
 
// 拉取分享列表
getShareList: function() {
  var that = this;
  wx.request({
    url: 'http://1.15.55.133:8080/share/recommand',
    success(res) {
      console.log("getShareLists: ", res);
      that.setData({//获取数据 更新列表
        feed: res.data.data          
      })
    }
  })
},

  getData: function(){ 
    var feed = util.getData2(); 
    console.log("loaddata"); 
    var feed_data = feed.data; 
    this.setData({ 
      feed:feed_data, 
      feed_length: feed_data.length 
    }); 
  }, 
  refresh: function(){ 
    wx.showToast({ 
      title: '刷新中', 
      icon: 'loading', 
      duration: 3000 
    }); 
    var feed = util.getData2(); 
    console.log("loaddata"); 
    var feed_data = feed.data; 
    this.setData({ 
      feed:feed_data, 
      feed_length: feed_data.length 
    }); 
    setTimeout(function(){ 
      wx.showToast({ 
        title: '刷新成功', 
        icon: 'success', 
        duration: 2000 
      }) 
    },3000) 
 
  }, 
 
  //使用本地 fake 数据实现继续加载效果 
  nextLoad: function(){ 
    wx.showToast({ 
      title: '加载中', 
      icon: 'loading', 
      duration: 4000 
    }) 
    var next = util.getNext(); 
    console.log("continueload"); 
    var next_data = next.data; 
    this.setData({ 
      feed: this.data.feed.concat(next_data), 
      feed_length: this.data.feed_length + next_data.length 
    }); 
    setTimeout(function(){ 
      wx.showToast({ 
        title: '加载成功', 
        icon: 'success', 
        duration: 2000 
      }) 
    },3000) 
  } ,
  
  // 分享功能
  onShareAppMessage: function ({from}) {
    console.log(from);
    if(from === 'button'){
      return {
        title: '来自用户XX的转发',
        page: '/pages/ANS1/ans1',
        imageUrl: '/images/kindle.jpg'
      }
    }else {
      return {
        title: '来自用户XX的转发',
        page: '/pages/ANS1/ans1',
        imageUrl: '/images/kindle.jpg'
      }
    }
  },

  // 首页搜索功能
  toSearch: function(){
    wx.navigateTo ({
      url: '/pages/search/search'
    })
  }
 
}) 